package com.hnkywl.sts.dao.kcgl;

import com.hnkywl.sts.dao.base.RegionDao;
import com.hnkywl.sts.entity.cggl.Dhd;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.Criteria;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-5-9
 * Time: 下午9:07
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class DhdDao extends SimpleHibernateTemplate<Dhd, Long> {
    public Page<Dhd> findDhdList(Long cgdId, Long cgdmxId, int pageNo, int pageSize) {
        Page<Dhd> page = new Page<Dhd>();
        page.setAutoCount(true);
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setOrder("desc");
        page.setOrderBy("dateCreated");

        Criteria criteria = getSession().createCriteria(this.entityClass)
                .createAlias("wz", "wz", CriteriaSpecification.LEFT_JOIN)
                .createAlias("cgdmx", "cgdmx", CriteriaSpecification.LEFT_JOIN)
                .setProjection(Projections.projectionList()
                        .add(Property.forName("id").as("id"))
                        .add(Property.forName("wz.id").as("wz.id"))
                        .add(Property.forName("wz.name").as("wz.name"))
                        .add(Property.forName("wz.code").as("wz.code"))
                        .add(Property.forName("wz.ggxh").as("wz.ggxh"))
                        .add(Property.forName("wz.dw").as("wz.dw"))
                        .add(Property.forName("cgdmx.id").as("cgdmxId"))
                        .add(Property.forName("cd").as("cd"))
                        .add(Property.forName("js").as("js"))
                        .add(Property.forName("cz").as("cz"))
                        .add(Property.forName("sssl").as("sssl"))
                        .add(Property.forName("dhrq").as("dhrq"))
                        .add(Property.forName("ysjl").as("ysjl"))
                        .add(Property.forName("zt").as("zt"))
//                        .add(Projections.sqlProjection("zt as zt", new String[]{"zt"}, new Type[]{Hibernate.STRING}))
                        .add(Property.forName("lastUpdated"))
                )
                .setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
        if (cgdmxId != null) {
            criteria.add(Restrictions.eq("cgdmx.id", cgdmxId));
        }
        criteria.add(Restrictions.eq("cgd.id", cgdId));
        RegionDao.createDataRegion(criteria);
        Page list = this.findByCriteria(page, criteria);
        return list;
    }

    public Long findDhdByCgdmxId(Long cgdmxId) {
        String sql = " select id from t_biz_dhd where cgdmx_id = ?";
        Object result = createSQLQuery(sql).setParameter(0, cgdmxId).uniqueResult();
        if (result != null) {
            return ((BigDecimal) result).longValue();
        } else {
            return -1l;
        }
    }
}
